<?php
class DB_Firebird
{
    private $link, $result, $db_host, $db_user, $db_pass, $i_write, $i_read;

    public function __construct($db_host, $db_user, $db_pass)
    {
        $this->db_host = $db_host;
        $this->db_user = $db_user;
        $this->db_pass = $db_pass;
        $this->connect();
        $this->i_read   = IBASE_READ || IBASE_COMMITTED || IBASE_REC_NO_VERSION || IBASE_WAIT;
        $this->i_write  = IBASE_WRITE || IBASE_COMMITTED || IBASE_REC_NO_VERSION || IBASE_WAIT;
    }

    public function __destruct()
    {
        $this->closeConnect();
    }
    

/*
 * connects to the database
 */
	private function connect()
	{   
            if (!is_resource($this->link))
            {
                $this->link = ibase_pconnect($this->db_host, $this->db_user, $this->db_pass);
                
                if (is_resource($this->link))
                {
                    return true;
                }
                return false;
            }

            return true;
	}
        

/*
 * closes the database connection
 */
	private function closeConnect()
	{
		if (is_resource($this->link))
		{
                    ibase_close($this->link);
		}
	}
        
	
/*
 * returns the result of a database query
 */
	public function qres($query = '', $transaction = '')
	{
                if ($this->connect())
                {
                    $res = array();
                    
                    if ('WRITE' == $transaction)
                    {
                        $argTrans = $this->i_write;
                    }
                    else if ('READ' == $transaction)
                    {
                        $argTrans = $this->i_read;
                    }
                    
                    $trans          = ibase_trans($this->link, $argTrans);
                    $this->result   = ibase_query($trans, $query);
                    
                    if (!is_resource($this->result) || false === $this->result)
                    {
                        return false;
                    }

                    while ($rows = ibase_fetch_object($this->result))
                    {
                        $res[] = $rows;
                    }
                    
                    $resCommit = ibase_commit($trans);
                    ibase_free_result($this->result);      
                
                    if ($resCommit)
                    {
                        return $res;
                    }
                }

                return false;
	}
        
        
/* For Execute procedure unanswered */
        public function exec($query = '', $transaction = '')
	{
            if ($this->connect())
            {
                if ('WRITE' == $transaction)
                {
                    $argTrans = $this->i_write;
                }
                else if ('READ' == $transaction)
                {
                    $argTrans = $this->i_read;
                }

                $trans      = ibase_trans($this->link, $argTrans);
		$resExec    = ibase_query($trans, $query);
                $resCommit  = ibase_commit($trans);
                
                if ($resCommit)
                {
                    return $resExec;
                }
            }
                
            return false;
	}
}
?>
